//
//  SLOrderViewController.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/20.
//


#import "SLOrderViewController.h"
#import <YYKit.h>
#import <Masonry.h>


@interface SLOrderViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *data;
@end

@implementation SLOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showNavBar:@"Order List"];
    [self hidenBackButton];
    self.navBar.theme = NavBarThemeDark;
    
    self.data = @[
        @"All Order",
        @"Under Way",
        @"Pending Repaymnet",
        @"Completed",
    ].mutableCopy;
    
    self.view.backgroundColor = UIColorHex(#F5F5F5);
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view).inset(0);
        make.top.equalTo(self.navBar.mas_bottom).inset(0);
        make.bottom.equalTo(self.view).inset(0);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UITableViewHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"UITableViewHeaderFooterView"];
    header.backgroundColor = UIColor.clearColor;
    header.contentView.backgroundColor = UIColorHex(#1B1B1B);
    UIView *bgView = [header viewWithTag:1000];
    if (!bgView) {
        bgView = [UIView new];
        bgView.tag = 1000;
        bgView.backgroundColor = UIColor.whiteColor;
        [header addSubview:bgView];
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(header);
        }];
    }
    
    UIImageView *iv = [header viewWithTag:1001];
    if (!iv) {
        iv = [UIImageView new];
        iv.tag = 1001;
        iv.image = [UIImage imageNamed:@"icon_circle_bar"];
        iv.contentMode = UIViewContentModeScaleAspectFit;
        [header addSubview:iv];
        [iv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(header).inset(20);
            make.bottom.equalTo(header).inset(10);
            make.height.mas_equalTo(25);
        }];
    }
    
    return header;
}
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    [header layoutIfNeeded];
    UIView *bgView = [header viewWithTag:1000];
    [bgView roundTopCornerRadius:20];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = self.data[indexPath.row];
    cell.backgroundColor = UIColor.clearColor;
    cell.contentView.backgroundColor = UIColor.whiteColor;
    UIView *accessory = [UIView new];
    accessory.frame = CGRectMake(0, 0, 40, 40);
    UIImageView *iv = [UIImageView new];
    iv.image = [UIImage imageNamed:@"btn_more_pink"];
    iv.contentMode = UIViewContentModeScaleAspectFit;
    [accessory addSubview:iv];
    [iv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(accessory);
        make.width.height.mas_equalTo(22);
    }];
    cell.accessoryView = indexPath.row == 0 ? nil : accessory;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 25+21;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return aspectValue(59);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [SLRoutePush route:@"SLOrderListContainerViewController" parameter:@{@"curIndex": @(indexPath.row)}];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:UITableViewHeaderFooterView.class forHeaderFooterViewReuseIdentifier:@"UITableViewHeaderFooterView"];
        [_tableView registerClass:UITableViewCell.class forCellReuseIdentifier:@"UITableViewCell"];
        _tableView.tableHeaderView = [self tableHeaderView];
    }
    return _tableView;
}
- (UIView *)tableHeaderView {
    UIView *head = [UIView new];
    
    UIImageView *iv = [UIImageView new];
    iv.backgroundColor = UIColorHex(#1B1B1B);
    iv.image = [UIImage imageNamed:@"icon_order_head"];
    iv.contentMode = UIViewContentModeScaleAspectFit;
    [head addSubview:iv];
    [iv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(head);
        make.height.mas_equalTo(304);
    }];
    head.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 300);
    
    return head;
}

@end
